package com.chis.ms;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class YaoJian {

    private String tmp;

    public YaoJian() {
    }

    public static void main(String[] args) throws Exception {
        String mainpath = "C:\\Users\\wlj\\Desktop\\vtts\\2015";
//        String mainpath = args[0];

        File file = new File(mainpath);
        File[] files = file.listFiles();
        for (File file2 : files) {
            File[] zipfiles = file2.listFiles();
            for (File file3 : zipfiles) {

                String zippath = file3.getAbsolutePath();
                if(zippath.endsWith("xml")){
                    continue;
                }
                String xmlpath = file3.getAbsolutePath().replace(file3.getName(),"data");
                unzip(zippath,xmlpath);
                String filepath = xmlpath+"\\data.xml";
                File data = dealcom(filepath);

                file3.delete();
                zip(zippath,data);
                data.delete();

            }
        }

    }




    public static File dealcom(String path) throws Exception {

        File file1 = new File(path);
        FileReader m = null;
        try {
            m = new FileReader(file1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(m);
        String newfile = path.replace("data.xml","new.xml");
//        FileWriter fileWriter = new FileWriter(newfile);

        FileOutputStream writerStream = new FileOutputStream(newfile);
        BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));

        boolean boo = false;
        boolean fff= false;
        String eventid = null;
        while (true) {
            String nextline = null;
            try {
                nextline = reader.readLine();
                if (nextline != null && nextline.length() > 8) {
                    if(nextline.contains("eventID")){
                        eventid = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                    }

                    if(nextline.contains("91310113MA1GPW779M")){
                        boo = true;
                        fff = true;
                    }
                    if(boo && nextline.contains("康希诺生物股份公司")){
                        String tmp = nextline.replace("康希诺生物股份公司","上海上药康希诺生物制药有限公司");
                        boo = false;
                        fileWriter.append(tmp).append("\r\n");
                    } else {
                        fileWriter.append(nextline).append("\r\n");

                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nextline == null) break;
        }
        fileWriter.close();

        if(fff){
            System.out.println(eventid);
        }


        String dd = newfile.substring(0,newfile.indexOf("data"))+"data.xml";
//        System.out.println(newfile);
//        System.out.println(dd);

        File filea = new File(newfile);
        File fileb = new File(dd);

        filea.renameTo(fileb);

        return fileb;


    }

    public static void unzip(String zipFilePath, String desDirectory) throws Exception {

        File desDir = new File(desDirectory);
        if (!desDir.exists()) {
            boolean mkdirSuccess = desDir.mkdir();
            if (!mkdirSuccess) {
                throw new Exception("创建解压目标文件夹失败");
            }
        }
        // 读入流
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
        // 遍历每一个文件
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) { // 文件夹
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                // 直接创建
                mkdir(new File(unzipFilePath));
            } else { // 文件
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                File file = new File(unzipFilePath);
                // 创建父目录
                mkdir(file.getParentFile());
                // 写出文件流
                BufferedOutputStream bufferedOutputStream =
                        new BufferedOutputStream(new FileOutputStream(unzipFilePath));
                byte[] bytes = new byte[1024];
                int readLen;
                while ((readLen = zipInputStream.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, readLen);
                }
                bufferedOutputStream.close();
            }
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }

    private static void mkdir(File file) {
        if (null == file || file.exists()) {
            return;
        }
        mkdir(file.getParentFile());
        file.mkdir();
    }

    public static void zip(String zipFileName, File file) throws IOException {
        ZipOutputStream zipOutputStream = null;
        try {
            // 输出流
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName), Charset.defaultCharset());
            // 遍历每一个文件，进行输出

                zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
                FileInputStream fileInputStream = new FileInputStream(file);
                int readLen;
                byte[] buffer = new byte[1024];
                while ((readLen = fileInputStream.read(buffer)) != -1) {
                    zipOutputStream.write(buffer, 0, readLen);
                }
                // 关闭流
                fileInputStream.close();
                zipOutputStream.closeEntry();

        } finally {
            if (null != zipOutputStream) {
                try {
                    zipOutputStream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }


}
